 <?php
class User extends Model_Abstract
{
	public $tableName = 'users';
	
	protected $_validation = array(
		'email' => array(
			array('is_email', '邮箱格式不正确'),
			array('max_length', '邮箱长度不能超过50', 50),
			array('not_empty', '请填写邮箱')
		),
		'nickname' => array(
			array('max_length', '长度不能超过30', 30),
			array('not_empty', '昵称不能为空')
		),
		'password' => array(
			array('not_empty', '密码不能为空'),
			array('min_length', '密码至少6位', 6),
			array('max_length', '密码长度不能超过50', 50),
			array('same', '两次输入密码不一致，请检查', 'password2')
		)
	);
	
	public function attributeLabels()
	{
		return array(
			'email' => '邮箱',
			'nickname' => '昵称',
			'password' => '密码',
			'address' => '城市地址',
			'checkCode' => '验证码'
		);
	}

	protected function validate()
	{
		$ret = true;
		$address = array(
			'province' => isset($this->attributes['province'])? $this->attributes['province'] : null,
			'city' => isset($this->attributes['city'])? $this->attributes['city'] : null,
			'area' => isset($this->attributes['area'])? $this->attributes['area'] : null
		);
		
		
		if(!Validator::validate_is_addressTab($address)){
			$this->_validationError['城市地址'][] = '请从下拉框选择城市地址';
			$ret = false;
		}
		
		if(empty($this->attributes['checkCode']) || !Util::accessCodeCheck($this->attributes['checkCode'])){
			$this->_validationError['验证码'][] = '验证码错误或者已过期, 请重新输入';
			$ret = false;
		}
		
		if(empty($this->attributes['terms'])){
			$this->_validationError['服务条款'][] = '如果您同意服务条款，请在前面打勾';
			$ret = false;
		}
		
		$user = $this->find('*', array('email' => $this->attributes['email']), null, null, null, true);
		if($user){
			$this->_validationError[$this->getAttributeLabel('email')][] = '您使用的邮箱已在本站注册过了，请更换另一个。你也可以选择其它方式登录';
			$ret = false;
		}
		
		$ret2 = parent::validate();
		
		return ($ret && $ret2);
	}

	protected function save($validate = true)
	{
		$this->_query[] = ' `create`=? ';
		$this->_vars[] = date('Y-m-d H:i:s');
		
		$this->attributes['password'] = Util::infoEncryption($this->attributes['password'], false);
		unset($this->attributes['password2']);
		unset($this->attributes['checkCode']);
		unset($this->attributes['terms']);
		
		$result = parent::save($validate);
		$this->attributes['password'] = Util::infoDecryption($this->attributes['password'], false);
		
		return $result;
	}
	
	protected function login()
	{
		//$user = $this->getUserByEmail();
		$user = $this->find('*', array('email' => $this->attributes['email']), null, null, null, true);
		$ret = $user;
		if(isset($_SESSION['showCheckCode']) && $_SESSION['showCheckCode']){
			if(empty($this->attributes['checkCode']) || !Util::accessCodeCheck($this->attributes['checkCode'])){
				$this->_validationError['验证码'][] = '验证码错误或者已过期, 请重新输入';
				$ret = false;
			}
		}
		
		if(!$user){
			$this->_validationError[$this->getAttributeLabel('email')][] = '电子邮箱不正确，请检查';
			$ret = false;
		}else{
			if(Util::infoDecryption($user['password'], false) !== $this->attributes['password']){
				$this->_validationError[$this->getAttributeLabel('password')][] = '密码不正确，请检查';
				$ret = false;
			}
		}
		
		return $ret;
	}


}